package org.farm2.tools.web;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.validator.routines.UrlValidator;
import org.farm2.tools.web.domain.WebDoc;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.safety.Safelist;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Farm2HttpUtils {

    public static boolean isHasIpOrPort(String url) {
        {
            String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
            Pattern pattern = Pattern.compile(ip);
            Matcher matcher = pattern.matcher(url);
            if (matcher.find()) {
                return true;
            }
        }
        {
            String port = ":([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])";
            Pattern pattern = Pattern.compile(port);
            Matcher matcher = pattern.matcher(url);
            if (matcher.find()) {
                return true;
            }
        }
        return false;
    }


    public static boolean isUrl(String url) {
        return UrlValidator.getInstance().isValid(url);
    }

    /**
     * 获得经过过滤的html
     *
     * @param url
     * @return
     */
    public static WebDoc getWebPageByUrl(String url) {
        OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).build();
        Request request = new Request.Builder().url(url)
                // 设置User-Agent来模拟浏览器
                .header("User-Agent", getAgent()).build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
            String html = response.body().string();
            // 解析HTML文档
            Document doc = Jsoup.parse(cleanHtml(html));
            //替换标签
            WebDoc wdoc = new WebDoc();

            wdoc.setBaseUrl(getBaseUrl(new URL(url), Jsoup.parse(html)));

            wdoc.setHtml(doc.html());
            return wdoc;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 獲得頁面的基準url
     *
     * @return
     * @throws MalformedURLException
     */
    private static URL getBaseUrl(URL url, Element document) throws MalformedURLException {
        String baseurl = null;
        Elements base = document.getElementsByTag("base");
        {
            if (base.size() > 0) {
                if (base.hasAttr("href")) {
                    baseurl = base.attr("href");
                }
            }
            if (baseurl == null) {
                baseurl = url.toString().substring(0, url.toString().indexOf(url.getPath()));
            }
            if (baseurl == null) {
                url.toString().substring(0, url.toString().lastIndexOf("/") + 1);
            }
            if (baseurl.lastIndexOf("/") == (baseurl.length() - 1)) {
                baseurl = baseurl.substring(0, baseurl.length() - 1);
            }
        }
        return new URL(baseurl);
    }

    public static String cleanHtml(String html) {
        Safelist safelist = Safelist.none()
                .addTags("span", "img", "h1", "h2", "h3", "ul", "li", "title") // 允许的标签
                .addAttributes("img", "src", "width", "height", "data-src") // 允许的 img 标签属性
                .addAttributes("span", "src") // 允许的 span 标签属性（可选）
                .preserveRelativeLinks(true); // 保留相对链接（可选）
        return Jsoup.clean(html, safelist);
    }


    public static void main(String[] args) {
        System.out.println(getWebPageByUrl("https://mp.weixin.qq.com/s/wp0_XBCk2s6-mv2yQrIR8g").getHtml());
    }

    public static String getImgUrl(String src, URL baseUrl) {
        if (src.indexOf("//") == 0) {
            src = baseUrl.getProtocol() + ":" + src;
        }
        if (src.toUpperCase().indexOf("HTTP") < 0) {
            if (src.indexOf("/") == 0) {
                src = baseUrl + src;
            } else {
                src = baseUrl + "/" + src;
            }
        }
        // url訪問不到資源或返回404就直接返回空
        if (!isLiveUrl(src)) {
            return null;
        }
        return src;
    }

    public static boolean isLiveUrl(String url) {
        try {
            URL remoteUrl = new URL(url);
            HttpURLConnection uConnection = (HttpURLConnection) remoteUrl.openConnection();
            uConnection.setConnectTimeout(1000);
            uConnection.setReadTimeout(10000);
            uConnection.connect();
            if (uConnection.getResponseCode() == 404) {
                return false;
            }
            return true;
        } catch (Exception e1) {
            return false;
        }
    }

    public static String getAgent() {
        return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";
    }
}
